﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using System.Data;
using ElingOA6_Admin_DSJCJ.Areas.Admin.Models;
using DevExpress.Web.Mvc;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 通知公告阅读
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class NoticeReadController : AdminBaseController
    {
        //
        // GET: /Admin/NoticeRead/
        private BLL.DocRecObject docRecObjectBLL = new BLL.DocRecObject();
        private BLL.Common commonBLL = new BLL.Common();

        public ActionResult Index()
        {
            string entityNo = GlobalConstant.ENTITYNO;
            string empNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string tableName = "v_document";
            int pageRowSize = 15;
            int pageIndex = 1;
            string fiedls = "*";
            string queryWhere = " FDocId IN ( SELECT FDocId FROM dbo.t_docRecObject WHERE FEntityNo='"+entityNo+"'";
            queryWhere += "AND FEmplNo='" + empNo + "' AND  (SELECT COUNT(*) FROM t_docReadLog WHERE FDocId=t_docRecObject.FDocId AND FEntityNo='" + entityNo + "' AND FEmplNo='" + empNo + "')=0";
            queryWhere += " AND  (SELECT COUNT(*) FROM dbo.t_document WHERE FDocId=t_docRecObject.FDocId AND FEntityNo='" + entityNo + "' AND FRecycle='Y')=0";
            queryWhere += " )";
            int total = 0;

            DataSet ds = commonBLL.GetListByPage(tableName, fiedls, pageRowSize, pageIndex, queryWhere, "FIsTop desc,FTopSeq asc,fdocid", 1, out total);
            List<DocumentGridModel> documentAllList = new List<DocumentGridModel>();
            List<DocumentGridModel> documentList = ViewDataRowToModel.DataTableToListDocuemt(ds.Tables[0]);

            for (int i = 0; i < total; i++)
            {
                documentAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (documentList.Count > i)
                {
                    documentAllList[((pageIndex - 1) * pageRowSize) + i] = documentList[i];
                }
            }

            return View(documentAllList);
        }
        public ActionResult NoticeListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());

            
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    case "REFRESH":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string entityNo = GlobalConstant.ENTITYNO;
            string empNo = OperateSession.get(GlobalConstant.PARAM_FEMPLNO);
            string tableName = "v_document";
            string fiedls = "*";
            string queryWhere = " FDocId IN ( SELECT FDocId FROM dbo.t_docRecObject WHERE FEntityNo='" + entityNo + "'";
            queryWhere += "AND FEmplNo='" + empNo + "' AND  (SELECT COUNT(*) FROM t_docReadLog WHERE FDocId=t_docRecObject.FDocId AND FEntityNo='" + entityNo + "' AND FEmplNo='" + empNo + "')=0";
            queryWhere += " AND (SELECT COUNT(*) FROM dbo.t_document WHERE FDocId=t_docRecObject.FDocId AND FEntityNo='" + entityNo + "' AND FRecycle='Y')=0";
            queryWhere += " )";
            int total = 0;

            DataSet ds = commonBLL.GetListByPage(tableName, fiedls, pageRowSize, pageIndex, queryWhere, "FIsTop desc,FTopSeq asc,fdocid", 1, out total);
            List<DocumentGridModel> documentAllList = new List<DocumentGridModel>();
            List<DocumentGridModel> documentList = ViewDataRowToModel.DataTableToListDocuemt(ds.Tables[0]);

            for (int i = 0; i < total; i++)
            {
                documentAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (documentList.Count > i)
                {
                    documentAllList[((pageIndex - 1) * pageRowSize) + i] = documentList[i];
                }
            }

            return PartialView(documentAllList);
        }
    }
}
